################################################################################
##
## Purpose: This script creates SI Figure 14
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/finalData.RData
##
## Output Files:
##  - ./output/figures/SI_figure_14.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(ggrepel)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

load('./data/prepped/finalData.RData')

pdf('./output/figures/SI_figure_14.pdf',width = 8,height = 5)
utterance_level %>%
  select(docID,yellenTime,date,chamber,matches('SENT_'),-matches('comb|LIKELY|UNSUB|INCOH|_lag')) %>%
  group_by(docID,date,yellenTime,chamber) %>%
  mutate(n = n()) %>%
  summarise_all(mean) %>%
  ungroup() %>%
  mutate(yellenTime = ifelse(date < as.Date('2014-01-01'),'Pre',
                             ifelse(yellenTime,'Yellen','Post'))) %>%
  ungroup() %>%
  pivot_longer(cols = starts_with("SENT_")) %>%
  drop_na(value) %>%
  mutate(name = gsub('SENT','',gsub('_',' ',name))) %>%
  ggplot(aes(x = date,y = value,group = yellenTime,size = n)) + 
  geom_point(shape = 21) + 
  geom_smooth(show.legend = F,method = 'lm',se = F,formula = 'y ~ poly(x,1)') + 
  annotate(geom = 'rect',xmin = as.Date('2014-01-01'),xmax = as.Date('2018-01-01'),
           ymin = -Inf,ymax = Inf,
           alpha = .2,fill = 'grey50') +
  geom_vline(xintercept = as.Date(c('2014-01-01','2018-01-01'))) + 
  annotate(geom = 'text',x = as.Date('2016-01-01'),y = Inf,label = 'Yellen',
           vjust = 1) + 
  theme_bw() + 
  theme(axis.text = element_text(size = 7),legend.position = 'bottom') + 
  facet_wrap(~name,scales = 'free') + 
  labs(x = 'Date',y = 'Predicted proportion')
dev.off()

# EOF